

/** 
 * 为了进一步减少主模块代码量, 把总市值的计算也搬过来, 主模块读个文件就可以取得中间价的总市值
 */
export async function main(ns) {
  // 所有的股票
  let symbols = ns.stock.getSymbols();
  for(let i=0;i<symbols.length;++i)
  {
    const SYM = symbols[i];
    let historyData={minPrice:0,maxPrice:0,middlePrice:0};//minPrice,maxPrice,middlePrice
    let filePath = "/stock/history/history_"+SYM+".txt";
    // 1.读文件
    if(ns.fileExists(filePath))
    {
      // 读取
      var strRead = ns.read(filePath);
      //ns.tprint("文件内容:\n",strRead);
      // 字符串分割:
      let arr1 = strRead.split("\n");
      for(var index in arr1)
      {
        if(0==arr1[index].length)continue;
        // ns.tprint("\t",arr1[index]);
        // 进一步分割字符串:
        let arr2 = arr1[index].split(":");
        if("minPrice"== arr2[0])
        {
          // ns.tprint("字符串0:",arr2[0],"正在分割的字串:",arr1[index]);
          historyData.minPrice = Number(arr2[1]);
        }
        else if("maxPrice"== arr2[0])
        {
          historyData.maxPrice = Number(arr2[1]);
        }
        else if("middlePrice"== arr2[0])
        {
          historyData.middlePrice = Number(arr2[1]);
        }
      }
      // ns.tprint("读文件的结果:",historyData);
      if(!historyData.minPrice<0 && !historyData.minPrice>0)
      {
        // ns.tprint("minPrice不是数字:",historyData.minPrice);
      }
      if(historyData.minPrice<0)
      {
        // ns.tprint("historyData.minPrice<0",historyData.minPrice);
      }
      if(isNaN(historyData.minPrice))
      {
        // ns.tprint("historyData.minPrice isNAN",historyData.minPrice," , ",historyData.minPrice );
        historyData.minPrice=0;
      }
      //historyData.minPrice = currentPrice;
      //historyData.maxPrice = currentPrice;
      //historyData.middlePrice = currentPrice;
      // END 有文件的情况
    }
    else
    {
      // 首次运行的初始化
      let currentPrice = ns.stock.getPrice(SYM);
      historyData.minPrice = currentPrice;
      historyData.maxPrice = currentPrice;
      historyData.middlePrice = currentPrice;
    }
    // END 读文件

    // 2.取值
    var currentPrice = ns.stock.getPrice(SYM);
    // ns.tprint("currentPrice:",currentPrice);

    // 3.比对值,算出结果
    if(0==historyData.minPrice)
    {
      historyData.minPrice = currentPrice;
    }
    if(historyData.minPrice>currentPrice)
    {
      historyData.minPrice = currentPrice;
    }
    if(historyData.maxPrice<currentPrice)
    {
      historyData.maxPrice=currentPrice;
    }
    historyData.middlePrice=(historyData.minPrice+historyData.maxPrice)/2;


    // 4.更新文件
    // 整合字符串
    let strData = "";
    for(const property in historyData)
    {
      strData+=property+":"+historyData[property];
      strData+='\n';
    }
    // ns.tprint("写文件前输出一下:",historyData);
    // ns.tprint("写文件前输出一下2:\n",strData);
    ns.write(filePath,strData,"w");
    // 为方便读取, 多写一份只有中间价的
    // 为了进一步减少主模块代码量, 把总市值的计算也搬过来
    var maxShares = ns.stock.getMaxShares(SYM);// 总手数, 以免一口气买到上限
    // ns.write("/stock/history/history_"+SYM+"_middlePrice.txt",historyData.middlePrice,"w");
    ns.write("/stock/history/history_"+SYM+"_middle_position.txt",historyData.middlePrice*maxShares,"w");
  }
}

// 原来有加载文件的功能
// import { Dashboard } from "/ui-example/components/Dashboard/Dashboard";